﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CollegeShop.Api.Framework;

namespace CollegeShop.Api.BaseComponent
{
    public abstract class IComponent
    {
        protected static IComponent instance = null;
        protected static object key = new object();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="loadAction"></param>
        /// <param name="duration">单位：秒</param>
        /// <returns></returns>
        protected T WithCache<T>(string key, Func<T> loadAction, int duration)
               where T : class
        {
            T data = CacheBag.GetCache<T>(key);
            if (data == null)
            {
                data = loadAction.Invoke();
                if (data != null)
                {
                    return CacheBag.Add<T>(key, data, CacheExpireStrategy.Absolute, new TimeSpan(0, 0, duration));
                }
            }
            return data;
        }

        public void RemoveCache(string key)
        {
            CacheBag.RemoveCache(key);
        }

    }
}
